package com.gxa.pingan.controller;


import com.aliyuncs.CommonRequest;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.gxa.pingan.common.ExceptionCode;
import com.gxa.pingan.common.JsonResult;
import com.gxa.pingan.exception.BusinessException;
import com.gxa.pingan.utils.StringUtils;
import io.swagger.annotations.Api;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.Random;

/**
 * @version fi
 * @Author lonny
 * @Date 2021/10/28 18:02
 */
@ResponseBody
@Controller

@Api(value = "验证码", tags = "验证码的接口")
public class SmsController {
    private static final Integer NUM = 7;

    @RequestMapping(value = "/SMS", method = {RequestMethod.GET, RequestMethod.POST})
    public Map<String, Object> doPost(HttpServletRequest req, String phoneOrEmail) throws Exception {
        //        获取手机号
//        通过正则校验符合手机号才发送短信，不符合返回前端消息
        JsonResult jsonResult = new JsonResult();
        if (!StringUtils.isEmpty(phoneOrEmail)) {
            if (StringUtils.isPhoneNumber(phoneOrEmail) || StringUtils.isEmail(phoneOrEmail)) {
                DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI5tGSEWzS9RM8EjC2w8bK", "IaTDtee5wde5RLvl3qIEXBZxbcMPOm");
                new DefaultAcsClient(profile);
                CommonRequest request = new CommonRequest();
                request.setSysMethod(MethodType.POST);
                request.setSysDomain("dysmsapi.aliyuncs.com");
                request.setSysVersion("2017-05-25");
                request.setSysAction("SendSms");
                request.putQueryParameter("RegionId", "cn-hangzhou");
//        前端获取的手机号
                request.putQueryParameter("PhoneNumbers", phoneOrEmail);
//        签名模板
                request.putQueryParameter("SignName", "金融保险");
//        短信模板
                request.putQueryParameter("TemplateCode", "SMS_204986028");
//        生产6位的随机数(验证码）
                StringBuilder builder = new StringBuilder();
                for (int i = 0; i < NUM; i++) {
                    builder.append(new Random().nextInt(9));
                }
//              记录验证码到session
                HttpSession session = req.getSession();
                session.setAttribute("captcha", builder.toString());
                session.setAttribute("phoneOrEmail", phoneOrEmail);
//        短信没有的时候直接通过控制台获取随机验证码
                System.out.println(builder);
                jsonResult.setCode("1");
                jsonResult.put("【金融保险】您的账号正在进行安全验证，验证码10分钟内有效，如非本人操作，请及时修改密码,验证码: ", builder);
            } else {
                throw new BusinessException(ExceptionCode.EMAIL_OR_ERROR);
            }
        } else {
            throw new BusinessException(ExceptionCode.PARAM_ERROR);
        }
        return jsonResult.getDataMap();
    }

}



